

var newer = newer || {};
newer.gd = newer.gd || {};


newer.CirclesDiv = function(o) {
	this.domNode = o.domNode;
	this.prt = o.prt;
	
	this.init();
};

newer.CirclesDiv.prototype = {
	
	init: function() {
		this.domNode.html("");
		this.refresh();
	},
	
	refresh: function() {
		this.update();
	},
	
	update: function() {
		this.domNode.html("");
		this.domNode.append('<div id="joined-circles-div" class="circle-gallery-area"></div>');
		this.domNode.append('<div id="not-joined-circles-div" class="circle-gallery-area"></div>');
		this.circlesNode = $("#joined-circles-div");
		this.circlesNode0 = $("#not-joined-circles-div");
		
		this.showCircles(this.circlesNode,"0");
		this.showCircles(this.circlesNode0,"1");
	},
	
	updateCirclePhotoSrc: function(circle) {
		if(circle.hasPhoto==true) {
			$("img#c"+circle.id).attr("src", "/Circles/displayCirclePhoto?cid="+circle.id);
		} else if (circle.hasMap == true) {
			$("img#c"+circle.id).attr("src", "/Circles/displayCircleMap?cid="+circle.id);
		} else {
			$.post("/Circles/updateCircleMap?cid="+circle.id, {}, function(){
				circle.hasMap = true;
				$("img#c"+circle.id).attr("src", "/Circles/displayCircleMap?cid="+circle.id);
			})
		}
	},
	
	showCircles: function(node,type) {
		var self = this;
		
		if (type == "0") {
			node.html("您加入的小组:<br/>");
		} else if (type == "1") {
			node.html("您未加入的小组:<br/>");
		}
			
		$.post("/Circles/getCirclesJSON", {"pid":newer.gd.people.id, "type":type}, function(result) {
			if (result.error == "invalid pid") {
				//do something
			} else if (result.error == "no such circles") {
				node.append("There are no Circles.<br/>");
			} else {
				node.append('<div id="circle-table-'+type+'t"></div>');
				$.each(result, function(index, circle) {
					$('#circle-table-'+type+'t').append('<div style="display:inline-block;vertical-align:bottom;" id="c'+circle.id+'"></div>');
					new newer.CircleCard({
						domNode: $("#c"+circle.id),
						idSuffix: "list-"+circle.id,
						circleInfo: circle
					});
				});
			}
		}, "json");
	}
};